package com.ruoyi.ruoyiyinzheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.api.domain.SysRole;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ruoyiyinzheng.domain.bo.StExperimentRequestBo;
import com.ruoyi.ruoyiyinzheng.domain.vo.StExperimentRequestVo;
import com.ruoyi.ruoyiyinzheng.domain.StExperimentRequest;
import com.ruoyi.ruoyiyinzheng.mapper.StExperimentRequestMapper;
import com.ruoyi.ruoyiyinzheng.service.IStExperimentRequestService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 实验要求Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-24
 */
@RequiredArgsConstructor
@Service
public class StExperimentRequestServiceImpl implements IStExperimentRequestService {

    private final StExperimentRequestMapper baseMapper;

    /**
     * 查询实验要求
     */
    @Override
    public StExperimentRequestVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询实验要求列表
     */
    @Override
    public TableDataInfo<StExperimentRequestVo> queryPageList(StExperimentRequestBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StExperimentRequest> lqw = buildQueryWrapper(bo);
        Page<StExperimentRequestVo> result = baseMapper.selectStExperimentRequestPageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
    @Override
    public List<String> selectRecipeNameListById(Long Id) {
        return baseMapper.selectStRecipeNameList(Id);
    }

    /**
     * 查询实验要求列表
     */
    @Override
    public List<StExperimentRequestVo> queryList(StExperimentRequestBo bo) {
        LambdaQueryWrapper<StExperimentRequest> lqw = buildQueryWrapper(bo);
        return baseMapper.selectStExperimentRequestList(lqw);
    }

    private LambdaQueryWrapper<StExperimentRequest> buildQueryWrapper(StExperimentRequestBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StExperimentRequest> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getExperimentNumber()), StExperimentRequest::getExperimentNumber, bo.getExperimentNumber());
        lqw.eq(bo.getColorSchemeId() != null, StExperimentRequest::getColorSchemeId, bo.getColorSchemeId());
        lqw.eq(StringUtils.isNotBlank(bo.getClorlNumber()), StExperimentRequest::getClorlNumber, bo.getClorlNumber());
        lqw.eq(bo.getArtId() != null, StExperimentRequest::getArtId, bo.getArtId());
        lqw.eq(bo.getMaterialId() != null, StExperimentRequest::getMaterialId, bo.getMaterialId());
        lqw.eq(StringUtils.isNotBlank(bo.getWeight()), StExperimentRequest::getWeight, bo.getWeight());
        lqw.eq(bo.getUnitId() != null, StExperimentRequest::getUnitId, bo.getUnitId());
        lqw.eq(bo.getFormulaId() != null, StExperimentRequest::getFormulaId, bo.getFormulaId());
        lqw.eq(StringUtils.isNotBlank(bo.getCo2()), StExperimentRequest::getCo2, bo.getCo2());
        lqw.eq(StringUtils.isNotBlank(bo.getTemperature()), StExperimentRequest::getTemperature, bo.getTemperature());
        lqw.eq(StringUtils.isNotBlank(bo.getPressure()), StExperimentRequest::getPressure, bo.getPressure());
        lqw.eq(StringUtils.isNotBlank(bo.getTimeConsuming()), StExperimentRequest::getTimeConsuming, bo.getTimeConsuming());
        return lqw;
    }

    /**
     * 新增实验要求
     */
    @Override
    public Boolean insertByBo(StExperimentRequestBo bo) {
        StExperimentRequest add = BeanUtil.toBean(bo, StExperimentRequest.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改实验要求
     */
    @Override
    public Boolean updateByBo(StExperimentRequestBo bo) {
        StExperimentRequest update = BeanUtil.toBean(bo, StExperimentRequest.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StExperimentRequest entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除实验要求
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
    @Override
    public int selectExperimentNumberCount(Long Id) {
        return baseMapper.selectExperimentNumberCount(Id);
    }
}
